var IM = require('../libs/IM.js');
var Config = require('../libs/config');
var Uploader = require('../libs/UploaderV5.js');
var app = getApp();
var c = require('../common.js');
var upimg = require('../upimg.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vhidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order_id=options.order_id;
    this.setData({ order_id: order_id})
    this.recorder = wx.getRecorderManager();
    //音频录制完成后执行上传操作
    this.recorder.onStart((res) => {
      console.debug('start record')
    });
    this.recorder.onStop((res) => {
      console.debug('end record')
      this.uploadFile(res.tempFilePath);
    });
    this.setData({ hiddeninfo: true});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  simpleUpload: function (e) {
    var path = 'demand/' + new Date().getTime() + '.png';
    var that = this;
    var returninfo = upimg.simpleUpload(
      path,
      function (res) {
        console.log(res);
        that.setData({
          arrayimg: res
        })
      },
      6
    );
  },
  //页面事件绑定，长按按钮的开始
  record_start: function () {
    var timedata = new Date();
    var timestring = timedata.getTime();
    this.setData({
      stime: timestring,
      hiddeninfo: false
    })
    this.recorder.start({
      format: "mp3"
    });
  },

  //页面事件绑定，长按按钮的结束
  record_end: function () {
    var longclick = this.data.longclick;
    if (longclick != 1) {
      wx.showToast({
        title: '录音时间短！',
        icon: 'none'
      })
      this.setData({
        hiddeninfo: true,

      });
      return;
    }
    var timedata = new Date();
    var timestring = timedata.getTime();
    var time = timestring - this.data.stime;
    var sint = parseInt(time / 1000);
    var mint = parseInt((time - sint * 1000) / 10);
    var long = sint * 20 + 'rpx';
    this.setData({
      longclick: 0,
      sint: sint,
      mint: mint,
      long: long,
      hiddeninfo: true,
      vhidden: false,
      longtime: time
    });
    this.recorder.stop();
  },
  //长按
  simpleRecorder: function () {
    this.setData({ longclick: 1 });
  },
  button:function(){
    
  },
  // 简单上传文件
  uploadFile(tempFilePath) {
    console.error('uploadFile', tempFilePath);
    var ntime = new Date();
    var time = ntime.getTime();
    var pathurl = '/demand/' + time + 'demand';
    var that = this;
    Uploader.upload(tempFilePath, function (result) {

      if (Config.COSv4) {
        var audioUrl = result.data.data.source_url.replace("http://", "https://")
      } else {
        var audioUrl = result.Location.replace("http://", "https://")
      }

      var audioUrlnew = audioUrl.replace('uploadFile', '')
      //更新demo的数据展示
      that.setData({
        sendAudioList: audioUrlnew,
        imgurl: pathurl + '.mp3'
      })
    }, '', pathurl);
  },

  avdiov: function () {
    const innerAudioContext = wx.createInnerAudioContext()
    var path = this.data.sendAudioList;

    console.log(path);
    
    innerAudioContext.autoplay = true;
    innerAudioContext.src = path;
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
  bindinputclick: function (e) {
    var inputvalue_time = e.detail.value;
    this.setData({
      inputvalue_time: inputvalue_time
    });
  },
  bindinputTextarea: function (e) {
    var inputvalue_textarea = e.detail.value;
    this.setData({
      inputvalue_textarea: inputvalue_textarea
    });
  },
  submit: function () {
    //order
    var order_id=this.data.order_id;
    //描述
    var inputvalue_textarea = this.data.inputvalue_textarea;
    //图片数组 url
    var arrayimg = this.data.arrayimg;
    //语音 url
    var sendAudioList = this.data.imgurl;
    //语音时长
    var longtime = Math.round(this.data.longtime/1000);
    //用户id
    // var uid = wx.getStorageSync('userInfo').user_id;
    var arrayimgnew = '';
    if (arrayimg) {
      for (var i = 0; i < arrayimg.length; i++) {
        arrayimgnew += arrayimg[i]['dbs'] + ',';
      }
      arrayimgnew = arrayimgnew.substring(0, arrayimgnew.length - 1);
    }
    

    if (typeof (sendAudioList)=='undefined'){
      sendAudioList='';
      longtime=0;
    }

     c.ajax(
       app.url+'toOrderIdChangOrderInfo',
       { order_id: order_id, arrayimg: arrayimgnew, sendAudioList: sendAudioList, longtime: longtime, inputvalue_textarea: inputvalue_textarea},
       res=>{
         if(res.data){
           wx.showToast({
             title: '添加成功',
             icon: 'none',
           })
         };
         setTimeout(function(){
           wx.navigateTo({
             url: '/pages/myorderguanli/myorderguanli',
             success: function (res) { },
             fail: function (res) { },
             complete: function (res) { },
           })
         },1000)

       }
     )
    
  }
})